package com.learn.practice.controller;

import com.learn.practice.liteflow.OrderContext;
import com.learn.practice.liteflow.UserContext;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.Future;

@RestController
public class ChainController {
    @Resource
    private FlowExecutor flowExecutor;

    @GetMapping("/flow/chain1")
    public String executeChain(){

        OrderContext orderContext = new OrderContext();
        orderContext.setOrderId("order1");
        orderContext.setPrice(200.00d);

        UserContext userContext = new UserContext();
        LiteflowResponse response = flowExecutor.execute2Resp("chain1","123", orderContext, userContext);
        if (!response.isSuccess()){
            Exception e = response.getCause();
            throw new RuntimeException(e);
        }
        return response.getExecuteStepStrWithTime();
    }

    @GetMapping("/flow/reload")
    public void reload(){
        flowExecutor.reloadRule();
    }
}
